using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

using Telerik.Web.UI;

/// <summary>
/// Summary description for Library
/// </summary>
public static class Library
{
    public static void ShowWarning(Label lblWarning, string message)
    {
        lblWarning.Text = message;
        lblWarning.Visible = true;
    }
    
    public static bool TextBoxIsEmpty(GridView GridView, GridViewUpdateEventArgs e, int CellIndex, int ControlIndex, Label lblWarning, string strMessage)
    {
        TextBox txtCategories = (TextBox)GridView.Rows[e.RowIndex].Cells[CellIndex].Controls[ControlIndex];
        if (txtCategories.Text == "")
        {
            Library.ShowWarning(lblWarning, strMessage);
            e.Cancel = true;
            return true;
        }
        else
            return false;
    }

    public static string NamNu(object GioiTinh)
    {
        if (GioiTinh == DBNull.Value)
            return "";

        if (((byte)GioiTinh) == 1)
            return "♂ Nam";
        else
            return "♀ Nữ";
    }

    public static string GetStringValue(object objField)
    {
        try
        {
            if (objField == DBNull.Value)
                return "";
            else
                return objField.ToString();
        }
        catch (Exception ex)
        {
            return "";
        }
    }

   /* public static string TenXaPhuongHuyenQuanTinhThanh(object TenTinhThanh, object TenHuyenQuan, object TenXaPhuong)
    {
        string strTenXaPhuongHuyenQuanTinhThanh = "";

        if (TenXaPhuong != DBNull.Value)
            strTenXaPhuongHuyenQuanTinhThanh += TenXaPhuong + " - ";
        if (TenHuyenQuan != DBNull.Value)
            strTenXaPhuongHuyenQuanTinhThanh += TenHuyenQuan + " - ";
        if (TenTinhThanh != DBNull.Value)
            strTenXaPhuongHuyenQuanTinhThanh += TenTinhThanh;

        return strTenXaPhuongHuyenQuanTinhThanh;
    }*/

    public static string GeneratePictureSymbol(object Picture)
    {
        if (Convert.IsDBNull(Picture))
            return "○";
        else
            return "☺";
    }

    public static bool CheckNullObject(object obj)
    {
        if (Convert.IsDBNull(obj))
            return false;
        else
            return true;
    }

    /*public static bool ValidateRangeDecimalValue(string userStringValue, decimal minValue, decimal maxValue)
    {
        try
        {
            decimal userValue = decimal.Parse(userStringValue, System.Globalization.NumberStyles.Currency);

            if (userValue >= minValue && userValue <= maxValue)
                return true;
            else
                return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    */
    public static string GetFilteringObjectDataSourceID(Page CurrentPage, Image imgFiltering, TextBox txtSearch)
    {
        if (!CurrentPage.IsPostBack) // Lần đầu Trang được mở
        {
            string strSearch;
            strSearch = CurrentPage.Request.QueryString["search"];

            if (strSearch == null)
            {
                strSearch = CurrentPage.Session["SearchString"].ToString();
                if (strSearch == null || strSearch == "")
                    imgFiltering.Visible = false;
                   
                else
                    imgFiltering.Visible = true;
            }
            else if (strSearch == "")
            {
                imgFiltering.Visible = false;
                CurrentPage.Session["SearchString"] = "";
            }
            else
            {
                imgFiltering.Visible = true;
                CurrentPage.Session["SearchString"] = strSearch; // Lưu lại vào biến tổng thể
            }
            txtSearch.Text = strSearch;
            if (txtSearch.Text == "")
                txtSearch.Visible = false;
            else
                txtSearch.Visible = true;
        }

        if (txtSearch.Text == null || txtSearch.Text == "")
            return "ObjectDataSource1";

        try
        {
            int n = Convert.ToInt32(txtSearch.Text);
            txtSearch.Text = n.ToString();
            return "odsbySubCateID"; ;
        }
        catch (Exception ex)
        {
            txtSearch.Text = txtSearch.Text.Trim();
            txtSearch.Text = txtSearch.Text.Replace("'", "''");
            txtSearch.Text = txtSearch.Text.Replace("*", "%");
            if (txtSearch.Text.IndexOf('%') == -1) // Không có dấu %
                txtSearch.Text = "%"+txtSearch.Text+"%";
            return "odsSubCateAndLikeName";
        }
    }
    public static string GetFilteringodsbookname(Page CurrentPage,  TextBox txttim)
    {
      
            //string strSearch;
            //strSearch = CurrentPage.Request.Form["Text1"];
             //strSearch=CurrentPage.Session["txttim"].ToString();           
           
        if (txttim.Text == null || txttim.Text == "")
            return "ObjectDataSource1";
        else
        {
            txttim.Text = txttim.Text.Trim();
            txttim.Text = txttim.Text.Replace("'", "''");
            txttim.Text = txttim.Text.Replace("*", "%");
            if (txttim.Text.IndexOf('%') == -1) // Không có dấu %
                txttim.Text = "%" + txttim.Text+"%";
            return "odsBookName";
        }   

     
    }

    public static string GetFilteringodsauthname(Page CurrentPage, TextBox txttim)
    {


        //string strSearch;
        //strSearch = CurrentPage.Request.Form["Text1"];
       // strSearch = CurrentPage.Session["txttim"].ToString();
        if (txttim.Text == null || txttim.Text == "")
            return "ObjectDataSource1";
        else
        {
            txttim.Text = txttim.Text.Trim();
            txttim.Text = txttim.Text.Replace("'", "''");
            txttim.Text = txttim.Text.Replace("*", "%");
            if (txttim.Text.IndexOf('%') == -1) // Không có dấu %
                txttim.Text = "%"+txttim.Text+"%";
            return "odslikeAuthName";
        }
    }
    public static string GetFilteringodsproductname(Page CurrentPage, TextBox txttim)
    {


        string strSearch;
        //strSearch = CurrentPage.Request.Form["Text1"];
        strSearch = CurrentPage.Session["txttim"].ToString();
        if (txttim.Text == null || txttim.Text == "")
            return "ObjectDataSource1";
        else
        {
            txttim.Text = txttim.Text.Trim();
            txttim.Text = txttim.Text.Replace("'", "''");
            txttim.Text = txttim.Text.Replace("*", "%");
            if (txttim.Text.IndexOf('%') == -1) // Không có dấu %
                txttim.Text = "%" + txttim.Text+"%";
            return "odslikeProName";
        }
    }
    public static string GetFilteringodsusername(Page CurrentPage, TextBox txtSearch)
    {


       // string strSearch;
        //strSearch = CurrentPage.Request.Form["Text1"];
       // strSearch = CurrentPage.Session["txtSearch"].ToString();
        if (txtSearch.Text == null || txtSearch.Text == "")
            return "ObjectDataSource1";
        else
        {
            txtSearch.Text = txtSearch.Text.Trim();
            txtSearch.Text = txtSearch.Text.Replace("'", "''");
            txtSearch.Text = txtSearch.Text.Replace("*", "%");
            if (txtSearch.Text.IndexOf('%') == -1) // Không có dấu %
                txtSearch.Text = "%"+txtSearch.Text+"%";
            return "odslikeusername";
        }
    }
    public static string GetFilteringodsemail(Page CurrentPage, TextBox txtSearch)
    {


        // string strSearch;
        //strSearch = CurrentPage.Request.Form["Text1"];
        // strSearch = CurrentPage.Session["txtSearch"].ToString();
        if (txtSearch.Text == null || txtSearch.Text == "")
            return "ObjectDataSource1";
        else
        {
            txtSearch.Text = txtSearch.Text.Trim();
            txtSearch.Text = txtSearch.Text.Replace("'", "''");
            txtSearch.Text = txtSearch.Text.Replace("*", "%");
            if (txtSearch.Text.IndexOf('%') == -1) // Không có dấu %
                txtSearch.Text = "%"+txtSearch.Text+"%";
            return "odsuserlikeemail";
        }
    }
    /*
    public static void SetSearchCriteria(Page CurrentPage, Image imgFiltering, TextBox txtSearch, ObjectDataSource odsObjectDataSource)
    {
        if (!CurrentPage.IsPostBack) // Lần đầu Trang được mở
        {
            string strSearch;
            strSearch = CurrentPage.Request.QueryString["search"];

            if (strSearch == null)
            {
                strSearch = CurrentPage.Session["SearchString"].ToString();
                if (strSearch == null || strSearch == "")
                    imgFiltering.Visible = false;
                else
                    imgFiltering.Visible = true;
            }
            else if (strSearch == "")
            {
                imgFiltering.Visible = false;
                CurrentPage.Session["SearchString"] = "";
            }
            else
            {
                imgFiltering.Visible = true;
                CurrentPage.Session["SearchString"] = strSearch; // Lưu lại vào biến tổng thể
            }
            txtSearch.Text = strSearch;
            if (txtSearch.Text == "")
                txtSearch.Visible = false;
            else
                txtSearch.Visible = true;
        }

        if (txtSearch.Text == null || txtSearch.Text == "")
        {
            odsObjectDataSource.FilterExpression = "";
            return;
        }

        try
        {
            int n = Convert.ToInt32(txtSearch.Text);
            txtSearch.Text = n.ToString();
            odsObjectDataSource.FilterExpression = "SoYeuID={0}";
        }
        catch (Exception ex)
        {
            txtSearch.Text = txtSearch.Text.Trim();
            txtSearch.Text = txtSearch.Text.Replace("'", "''");
            odsObjectDataSource.FilterExpression = "HoTen LIKE '{0}%'";
        }
    }*/

    //Replace Vietnames to Vietnamese UnSign
    public static string ConvertToUnSign(string text)
    {
        for (int i = 32; i < 48; i++)
        {
            text = text.Replace(((char)i).ToString(), " ");
        }
        text = text.Replace(".", "-");
        text = text.Replace(" ", "-");
        text = text.Replace(",", "-");
        text = text.Replace(";", "-");
        text = text.Replace(":", "-");

        Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");

        string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);
        return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
    }

    public static void BindToDropDownList(RadComboBox ddlCate)
    {
        DaiCuongMobileTableAdapters.tbCategoriesTableAdapter adtCategory = new DaiCuongMobileTableAdapters.tbCategoriesTableAdapter();
        DataTable dtCate = adtCategory.GetCategories();
        if (dtCate.Rows.Count > 0)
        {
            foreach (DataRow row in dtCate.Rows)
            {
                if (Convert.IsDBNull(row["ParentCategoryID"]))
                {
                    RadComboBoxItem item = new RadComboBoxItem();                   
                    item.Text = row["CategoryName"].ToString();
                    item.Value = row["CategoryID"].ToString();
                    ddlCate.Items.Add(item);

                    // Tìm chuyên mục con của nó
                    string ParentID = row["CategoryID"].ToString();
                    GetChildNode(ParentID, dtCate, ddlCate, 0);
                }
            }
        }
    }

    public static void GetChildNode(string ParentCateID, DataTable dtCateTemp, RadComboBox ddlChildCate, int Level)
    {
        string child = "";
        int level = Level + 1;
        for (int i = 0; i < level; i++)
        {
            child += "|--";
        }
        foreach (DataRow row in dtCateTemp.Rows)
        {
            if (row["ParentCategoryID"].ToString() == ParentCateID)
            {
                RadComboBoxItem childItem = new RadComboBoxItem();
                childItem.Text = child + " " + row["CategoryName"].ToString();
                childItem.Value = row["CategoryID"].ToString();
                ddlChildCate.Items.Add(childItem);

                // Đệ qui tìm mục con của nó
                string ParentID = row["CategoryID"].ToString();
                GetChildNode(ParentID, dtCateTemp, ddlChildCate, level);
            }
        }
        Level--;

    }
}
